from django.shortcuts import render

from rest_framework.views import APIView
from rest_framework.response import Response

from home.models import NavModel, BannerModel, CategoryModel
from home.ser import NavSer, BannerSer, CategorySer


class HomeView(APIView):
    def get(self, request):
        return Response("ok")
class ToNav(APIView):
    def get(self, request):
        topnav = NavModel.objects.filter(id_del=False,position=True).order_by("order")
        ser = NavSer(topnav, many=True)
        return Response({"msg":"ok","code":200,"data":ser.data})
class FootNav(APIView):
    def get(self, request):
        footpnav = NavModel.objects.filter(id_del=False,position=False).order_by("order")
        ser = NavSer(footpnav, many=True)
        return Response({"msg":"ok","code":200,"data":ser.data})
class Banner(APIView):
    def get(self, request):
        banner = BannerModel.objects.filter(id_del=False).order_by("order")
        ser = BannerSer(banner, many=True)
        return Response({"msg":"ok","code":200,"data":ser.data})
class Category(APIView):
    def get(self, request):
        cate = CategoryModel.objects.filter(id_del=False,parent=None).order_by("order")
        ser = CategorySer(cate, many=True)
        for i in ser.data:
            i["children"] = i["children"][:3]
        return Response({"msg":"ok","code":200,"data":ser.data})
class Info(APIView):
    def get(self, request):
        info = CategoryModel.objects.filter(id_del=False,id=request.query_params.get("id")).first()
        ser = CategorySer(info)
        return Response({"msg":"ok","code":200,"data":ser.data})
# Create your views here.
